home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0885.arc
/
SMALTAL3.LTG
< prev
Wrap
Text File
|
1986-02-27
|
5KB
|
133 lines
Smalltalk LISTING 3:
Speller Class Definition and Methods
Object subclass: #Speller
instanceVariableNames:
'wordList correctList ' "Valid word list and autocorrect list"
classVariableNames:
'Marker' "Char for marking misspelled words"
poolDictionaries: ''
Speller class methods
words: wordFile corrections: correctFile
"Create a new speller object, reading the word list
from wordFile and correction list from correctFile.
Also initialize the character that misspelled words
are marked with."
Marker := $*.
^self new initialize
words: wordFile;
corrections: correctFile
Speller methods
addCorrections: inStream
"Add correction word-pairs from the input stream to
the correction list. Each word pair is on its own line.
A line with only one word specifies a word to be deleted
rather than replaced."
|entry word|
[inStream atEnd]
whileFalse: [entry := ReadStream on: inStream nextLine.
word := entry nextWord.
entry nextGap.
entry atEnd
ifTrue: [self correct: word with: '']
ifFalse: [self correct: word
with: entry nextWord]]
addWord: aWord
"Add a word to the word list."
aWord notNil
ifTrue: [wordList add: aWord trimBlanks asUpperCase]
addWords: inStream
"Add words in the input stream to the word list"è [inStream atEnd]
whileFalse: [self addWord: inStream nextWord].
self addWord: ''
check: aWord
"Spell check a word and if misspelled, prompt the
user with the marked word. The user may hit CR
to output the marked word, erase the mark to accept
the word as is, or edit the word to correct it. If
the user accepts or edits the word, the wordList and
correctList are updated accordingly."
|word|
word := self spell: aWord.
(self isMarked: word)
ifTrue: [word := Prompter prompt: 'Suspect word'
default: word.
(self isMarked: word)
ifFalse: [self addWord: word.
word ~= aWord
ifTrue: [self correct: aWord
with: word]]].
^word
correct: badWord with: goodWord
"Add a word pair to the correction list."
((badWord notNil) and: [goodWord notNil])
ifTrue: [correctList at: badWord trimBlanks asUpperCase
put: goodWord trimBlanks asUpperCase]
corrections: correctFile
"Add correction pairs from the specified file to the
correction list."
self addCorrections: (Disk file: correctFile)
initialize
"Create word list and correction list objects for speller"
wordList := Set new.
correctList := Dictionary new.
isMarked: aWord
"Return true if aWord has been marked by the spell method"
^(aWord size > 0) and: [(aWord at: aWord size) = Marker]
matchCase: aWord using: modelWord
"Make aWord conform to the case/capitalization of
the model word."
|word|
(modelWord at: modelWord size) isUpperCase
ifTrue: [^aWord asUpperCase].è word := aWord asLowerCase.
(modelWord at: 1) isUpperCase
ifTrue: [word at: 1 put: (word at: 1) asUpperCase].
^word
spell: aWord
"Spell check a word: replace it if it has an entry
in correctList, otherwise mark it if not in WordList.
If the word is being replaced, make the case of the
replacement word correspond to that of the input word."
^self
matchCase:
(correctList at: aWord trimBlanks asUpperCase
ifAbsent:
[(wordList includes: aWord trimBlanks asUpperCase)
ifTrue: [^aWord]
ifFalse: [^aWord , (String with: Marker)]])
using: aWord
words: wordFile
"Add words from the specified file to the word list."
self addWords: (Disk file: wordFile)